import hashlib
import requests
import json
import urllib.parse
import csv
import time


# 此处填入自己的微信Authorization
auth = ''
# Authorization: wxmp.******
def xhs():
    user_input = input('输入需要搜索的关键词: \n')
    keyword = urllib.parse.quote(user_input)

    saveData = []
    for i in range(1,6):
        url = "https://www.xiaohongshu.com/fe_api/burdock/weixin/v2/search/notes?keyword={}&sortBy=hot_desc&page={}&pageSize=20&prependNoteIds=&needGifCover=true"
        url = url.format(keyword, i)
        # 对请求链接进行加密
        xsign = "X{0}".format(
            hashlib.md5("{0}WSUDD".format(url.replace("https://www.xiaohongshu.com", "")).encode("utf-8")).hexdigest())
        res = setxhs(url, xsign)
        time.sleep(1)
        notes = res['data']['notes']
        print(notes)
        for note in notes:
            saveData.append(note)
    print(saveData)
    csva(saveData,user_input)

def setxhs(url,xsign):
    headers = {
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-cn',
        'Connection': 'keep-alive',
        'Content-Type':'application/json',
        'Host': 'www.xiaohongshu.com',
        # 'Referer': 'https://servicewechat.com/wxb296433268a1c654/81/page-frame.html',
        'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.10(0x18000a28) NetType/WIFI Language/zh_CN',
        'Host':'www.xiaohongshu.com',
        'Authorization': auth,
        'X-Sign': xsign,
    }

    response = requests.get(
        url=url,
        headers=headers,
    )
    content = response.content
    # 转换成字符串
    string = content.decode('utf-8')
    # 把字符串转成python数据类型
    results = json.loads(string)
    print(results)
    return results

def csva(notes,keyword):
    file = open(keyword+'.csv', 'w', encoding='utf-8', newline='')
    # 先设置列名，并写入csv文件
    csv_writer = csv.DictWriter(file, fieldnames=['id', 'title', 'type', 'likes', 'isLiked', 'cover', 'time', 'comments', 'collects', 'user'])
    csv_writer.writeheader()
    for note in notes:
        csv_writer.writerow(note)

if __name__ == '__main__':
    xhs()